﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellExecutorManager.h"
#include "CellLoggerManager.h"
#include "CellException.h"
#include "CellCachedThreadPool.h"
#include "CellFixedThreadPool.h"
#include "CellScheduledThreadPoolExecutor.h"

namespace cell
{

template<> ExecutorManager* Singleton<ExecutorManager>::ms_Singleton = NULL;

//-----------------------------------------------------------------------
ExecutorManager* ExecutorManager::getSingletonPtr(void)
{
	return ms_Singleton;
}
ExecutorManager& ExecutorManager::getSingleton(void)
{  
	assert( ms_Singleton );
	return ( *ms_Singleton );  
}

//-----------------------------------------------------------------------
ExecutorManager::ExecutorManager(void)
{
	LoggerManager::getSingleton().log("ExecutorManager singleton created (" + cell::peek<ExecutorManager>(this) + ")");
}
//-----------------------------------------------------------------------
ExecutorManager::~ExecutorManager(void)
{
	LoggerManager::getSingleton().log("ExecutorManager singleton destroyed (" + cell::peek<ExecutorManager>(this) + ")");

	CachedPoolMap::iterator cpiter, cpend = mCachedThreadPools.end();
	for (cpiter = mCachedThreadPools.begin(); cpiter != cpend; ++cpiter)
	{
		CachedThreadPool* pool = cpiter->second;
		CELL_DELETE pool;
	}
	mCachedThreadPools.clear();

	FixedPoolMap::iterator fpiter, fpend = mFixedThreadPools.end();
	for (fpiter = mFixedThreadPools.begin(); fpiter != fpend; ++fpiter)
	{
		FixedThreadPool* pool = fpiter->second;
		CELL_DELETE pool;
	}
	mFixedThreadPools.clear();
}
//-----------------------------------------------------------------------
ExecutorService* ExecutorManager::createCachedThreadPool(const String& name, int coreSize, int maxSize)
{
	if (mCachedThreadPools.find(name) != mCachedThreadPools.end())
	{
		CELL_EXCEPT(
			Exception::ERR_DUPLICATE_ITEM,
			"A cached thread pool with the name " + name + " already exists",
			"ExecutorManager::createCachedThreadPool");
	}
	CachedThreadPool* pool = CELL_NEW CachedThreadPool(name, coreSize, maxSize);
	mCachedThreadPools.insert(CachedPoolMap::value_type(name, pool));
	return static_cast<ExecutorService*>(pool);
}
//-----------------------------------------------------------------------
void ExecutorManager::destroyCachedThreadPool(const String& name)
{
	CachedPoolMap::iterator iter = mCachedThreadPools.find(name);
	if (iter != mCachedThreadPools.end())
	{
		CachedThreadPool* pool = iter->second;
		mCachedThreadPools.erase(iter);

		CELL_DELETE pool;
	}
}
//-----------------------------------------------------------------------
ExecutorService* ExecutorManager::createFixedThreadPool(const String& name, int threads)
{
	if (mFixedThreadPools.find(name) != mFixedThreadPools.end())
	{
		CELL_EXCEPT(
			Exception::ERR_DUPLICATE_ITEM,
			"A fixed thread pool with the name " + name + " already exists",
			"ExecutorManager::createFixedThreadPool");
	}
	FixedThreadPool* pool = CELL_NEW FixedThreadPool(name, threads);
	mFixedThreadPools.insert(FixedPoolMap::value_type(name, pool));
	return static_cast<ExecutorService*>(pool);
}
//-----------------------------------------------------------------------
void ExecutorManager::destroyFixedThreadPool(const String& name)
{
	FixedPoolMap::iterator iter = mFixedThreadPools.find(name);
	if (iter != mFixedThreadPools.end())
	{
		FixedThreadPool* pool = iter->second;
		mFixedThreadPools.erase(iter);

		CELL_DELETE pool;
	}
}
//-----------------------------------------------------------------------
ScheduledThreadPoolExecutor* ExecutorManager::createScheduledThreadPool(
	const String& name, int corePoolSize, int maxPoolSize)
{
	if (mScheduledPools.find(name) != mScheduledPools.end())
	{
		CELL_EXCEPT(
			Exception::ERR_DUPLICATE_ITEM,
			"A scheduled thread pool with the name " + name + " already exists",
			"ExecutorManager::createScheduledThreadPool");
	}
	ScheduledThreadPoolExecutor* pool = CELL_NEW ScheduledThreadPoolExecutor(name, corePoolSize, maxPoolSize);
	mScheduledPools.insert(ScheduledPoolMap::value_type(name, pool));
	return pool;
}
//-----------------------------------------------------------------------
void ExecutorManager::destroyScheduledThreadPool(const String& name)
{
	ScheduledPoolMap::iterator iter = mScheduledPools.find(name);
	if (iter != mScheduledPools.end())
	{
		ScheduledThreadPoolExecutor* pool = iter->second;
		CELL_DELETE pool;
		mScheduledPools.erase(iter);
	}
}

} // end namespace cell
